/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Practica4.util;

/**
 *
 * @author miguel
 */
public class Lista {
    
    private Nodo cabeza;
    private Nodo cola;

    public Nodo getCabeza() {
        return cabeza;
    }

    public void setCabeza(Nodo cabeza) {
        this.cabeza = cabeza;
    }

    public Nodo getCola() {
        return cola;
    }

    public void setCola(Nodo cola) {
        this.cola = cola;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
    private int size;
    
    public Lista(int i){
        cabeza = new Nodo(i);
        cola = cabeza;
        size = 1;
    }
    
    public Nodo insertaIzquierda(Nodo ui,Nodo n){
        Nodo n1 = ui;
        while(n1.getBack() != null && n1.getValor()>=n.getValor()){
            n1 = n1.getBack();
        }
        if(n1.getBack()!=null){
            n.setBack(n1.getBack());
            n.setNext(n1);
            n1.getBack().setNext(n);
            n1.setBack(n);
        }
        else{
            n1.setBack(n);
            n.setNext(n1);
            cabeza = n;
        }
        
        size++;
        return n;
    }
    
    public Nodo insertaDerecha(Nodo ui, Nodo n){
        Nodo n1 = ui;
        while(n1.getNext()!=null && n.getValor()>n1.getValor()){
               n1 = n1.getNext();
        }
        
        if(n1.getNext()!=null){
            n.setNext(n1.getNext());
            n.setBack(n1);
            n1.getNext().setBack(n);
            n1.setNext(n);
        }else{
            n1.setNext(n);
            n.setBack(n1);
            cola = n;
        }
        size++;
        return n;
    }
    
    
    public int[] datos(){
        int[] datos = new int[size];
        Nodo n = cabeza;
        int i = 0;
        while(n!=null){
            datos[i++] = n.getValor();
            n = n.getNext();
        }
        return datos;
    }
    
}
